<?php

include_once F_SOURCE . 'launch/launch.model.class.php';

/**
 * 批量导入投放管理CTRL类
 * 
 * @author Dean
 *
 */
class importlaunchCtrl extends Ctrl{
	
	public function show() {
		$view = new View();
		
		$data = array(
			'op_select' => array('importlaunch'=>"class=select"),
		);
		
		$view->display($data, 'importlaunch_show.html');
	}

	public function import(){
		
		$view = new View();
		
		$url = page_save($_FILES['page'],F_UPLOAD);
		$urlArray = explode('@@',$url);
		
		//如果出现错误，进行错误提示
		if($urlArray[0]=='true'){
			$fileName = trim(substr(strrchr($urlArray[1], '/'), 1));
			$url = $urlArray[1];
			
			$this->showMessage('?do=importlaunch.waiting&url='.$fileName, 'upload_success');
		}else{
			$this->showMessage('?do=importlaunch.show', $urlArray[1]);
		}
		
		exit;
	}

	public function waiting(){
		$view = new View();
		$view->display(array(), 'importlaunch_waiting.html');
	}
	
	public function add(){
		
		$url = getPostStringParam('url');
		
		$handle = fopen(F_UPLOAD.$url, "r");
		$key = 0;
		while (($data = fgetcsv($handle, 0, ",")) !==FALSE) {
			$c = count($data);
			for ($x=0;$x<$c;$x++) $arrCSV[$key][$x] = $data[$x];
			$key++;
		}
		fclose($handle);
		
		$vbcNum = 0;
		$kwsNum = 0;
		$jsonIndex = 0;
		foreach ($arrCSV as $csv_key => $csv_value) {
			if(intval($csv_value[0])<=0)continue;
			$temp['id'] = "'".addslashes($csv_value[0])."'";
			$temp['name'] = "'".addslashes(formatToUTF8($csv_value[1]))."'";
			$temp['order'] = "'".addslashes(formatToUTF8($csv_value[2]))."'";
			$temp['activity'] = "'".addslashes(formatToUTF8($csv_value[3]))."'";
			$temp['adsnum'] = "'".addslashes($csv_value[4])."'";
			$temp['start'] = "'".addslashes(formatCsvDate($csv_value[5]))."'";
			$temp['end'] = "'".addslashes(formatCsvDate($csv_value[6]))."'";
			$temp['priority'] = "'".addslashes($csv_value[7])."'";
			$temp['daynumber'] = "'".addslashes($csv_value[8])."'";
// 			$temp['type'] = "'".addslashes(formatToUTF8($csv_value[9]))."'";
			$type = getAdsTypeByName($temp['name']);
			if($type=='vbc'){
				$vbcNum++;
			}else{
				$kwsNum++;
			}
			$temp['type'] = "'".getAdsTypeByName($temp['name'])."'";
			$temp['kind'] = "'".addslashes(formatToUTF8($csv_value[10]))."'";
			$temp['orientation'] = "'".addslashes(formatToUTF8($csv_value[11]))."'";
			$temp['size'] = "'".addslashes(formatToUTF8($csv_value[12]))."'";
			$temp['status'] = "'".addslashes(formatToUTF8($csv_value[13]))."'";
			$temp['op'] = "'o'";
			$temp['createTime'] = "'".getNowTime()."'";

			$strArr[$jsonIndex] = '('.implode(',', $temp).')';
			$jsonIndex++;
		}
		
		$fields = array(
			'`id`',
			'`name`',
			'`order`',
			'`activity`',
			'`adsnum`',
			'`start`',
			'`end`',
			'`priority`',
			'`daynumber`',
			'`type`',
			'`kind`',
			'`orientation`',
			'`size`',
			'`status`',		
			'`op`',
			'`createTime`',
		);
		
		$launchModel = new launchModel();
		$returnID = $launchModel->importDataToDB($fields, $strArr);
		
		if($returnID > 0){
			
			echo 'true@@?do=ads.show';
		}else{
			echo 'false@@插入数据失败！';
		}
		exit;
	}	
	
	public function vbcshow(){
		
		$view = new View();
		
		$pageIndex = ($_GET['pageIndex']=='' || $_GET['pageIndex']<1)?1:$_GET['pageIndex'];
		
		$launchModel = new launchModel();
		
		$number = $launchModel->import_countAll();
		
		$launchList = $launchModel->import_findAll(($pageIndex-1)*PageNum, PageNum);
		
		$page = pageCount($number, $pageIndex, '?do=importlaunch.vbcshow');
		
		$data = array(
				'op_select' => array('vbc'=>"class=select"),
				'launchList' => $launchList,
				'page' => $page
		);
		
		$view->display($data, 'importlaunch_vbcshow.html');
		
	}
	
	public function vbcinfo(){
		$view = new View();
		
		$id = $_GET['id'];
		
		$launchModel = new launchModel();
		$launchInfo = $launchModel->import_findOne($id);
		
		$data = array(
				'op' => 'update',
				'op_select' => array('vbc'=>"class=select"),
				'obj' => $launchInfo
		);
		
		$view->display($data, 'importlaunch_vbcview.html');
	}
}


?>